﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeIn.Core.Modules;
using CodeIn.Modules.Graph.Elements;
using CodeIn.Core;

namespace CodeIn.Modules.Graph.Modules.Ui.Flow {
	public class FlowTest : BaseModule {
		protected override void InitializeOverride() {
		}

		protected override bool CanExecuteOverride(string[] args) {
			if (args.Length < 3)
				return False("You must provide the node count as first param and link count as second one.");

			int i;
			if (!int.TryParse(args[1], out i))
				return False("First param must be an integer");
			if (!int.TryParse(args[2], out i))
				return False("Second param must be an integer");

			return true;
		}

		protected override void ExecuteOverride(string[] args) {
			int nodeCount = int.Parse(args[1]);
			int linkCount = int.Parse(args[2]);

			DataFlowGraph randomGraph = new DataFlowGraph();
			for (int i = 0; i < nodeCount; i++) {
				randomGraph.Add(new DataFlowNode());
			}

			Random rand = new Random();
			var nodes = randomGraph.Nodes;
			for (int i = 1; i < linkCount; i++) {
				DataFlowNode n1 = nodes[rand.Next(0, nodes.Length - 1)];
				DataFlowNode n2 = nodes[rand.Next(0, nodes.Length - 1)];

				if (n1 != n2)
					randomGraph.CreateLink(n1, n2);
			}

			DataFlowGraph oldGraph = GraphContext.Instance.CurrentGraph;
			GraphContext.Instance.CurrentGraph = randomGraph;
			ModuleManager.Instance.CallSync("flowview");
			GraphContext.Instance.CurrentGraph = oldGraph;
		}
	}
}
